function header(map) {
    var arr = [];
    arr.push('<?xml version="1.0" encoding="UTF-8"?>');
    arr.push('<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">');
    arr.push('<mapper namespace="' + map.attach.daoPackage + '.' + map.pascalName + 'Dao">');
    arr.push('');
    return arr.join("\r\n");
}

function mapping(map) {
    var arr = [];
    arr.push('');
    arr.push('    <resultMap type="' + map.attach.beanPackage + '.' + map.pascalName + '" id="' + map.camelName + '">');
    var columns = map.columns;
    for (var i = 0; i < columns.length; i++) {
        var column = columns[i];
        arr.push('        <result column="' + column.columnName + '" property="' + column.camelName + '" javaType="' + column.javaType + '"/>');
    }
    arr.push('    </resultMap>');
    arr.push('');
    arr.push('');
    return arr.join("\r\n");
}

function columns(map) {
    var arr = [];
    arr.push('    <sql id="columns">');

    var line = [];
    var columns = map.columns;
    for (var i = 0; i < columns.length;) {
        var column = columns[i];
        if (i % 3 === 0) {
            line.push('        ');
        }
        line.push('T1.' + column.columnName);
        i++;
        if (i < columns.length) {
            line.push(', ');
        }
        if (i === column.length || i % 3 === 0) {
            line.push('\r\n');
        }
    }
    arr.push(line.join(''));

    arr.push('    </sql>');
    arr.push('');
    return arr.join("\r\n");
}


function where(map) {
    var arr = [];
    arr.push('');
    arr.push('    <sql id="where_clause">');
    arr.push('        <if test="keyword != null and keyword != \'\'">');
    arr.push('            AND INSTR(T1.keyword, #{keyword}) > 0');
    arr.push('        </if>');

    var columns = map.columns;
    for (var i = 0; i < columns.length; i++) {
        var column = columns[i];
        if (column.javaType === 'String') {
            arr.push('        <if test="' + column.camelName + ' != null and ' + column.camelName + ' != \'\'">AND T1.' + column.columnName + ' = #{' + column.camelName + '}</if>')
        } else {
            arr.push('        <if test="' + column.camelName + ' != null">AND T1.' + column.columnName + ' = #{' + column.camelName + '}</if>');
        }
    }
    arr.push('    </sql>');
    arr.push('');
    return arr.join("\r\n");
}

function insert(map) {
    var arr = [];
    arr.push('');
    arr.push('');
    arr.push('    <!-- Insert -->');
    arr.push('    <insert id="insert" parameterType="' + map.attach.beanPackage + '.' + map.pascalName + '">');
    arr.push('        INSERT INTO ' + map.tableName + ' (');

    var line = [];
    var columns = map.columns;
    for (var i = 0; i < columns.length;) {
        var column = columns[i];
        if (i % 3 === 0) {
            line.push('            ');
        }
        line.push(column.columnName);
        i++;
        if (i < columns.length) {
            line.push(', ');
        }
        if (i === column.length || i % 3 === 0) {
            line.push('\r\n');
        }
    }
    arr.push(line.join(''));

    arr.push('        ) VALUES (');

    line = [];
    for (var i = 0; i < columns.length;) {
        var column = columns[i];
        if (i % 3 === 0) {
            line.push('            ');
        }
        line.push('#{' + column.camelName + '}');
        i++;
        if (i < columns.length) {
            line.push(', ');
        }
        if (i === column.length || i % 3 === 0) {
            line.push('\r\n');
        }
    }
    arr.push(line.join(''));

    arr.push('        )');
    arr.push('    </insert>');
    arr.push('');
    return arr.join("\r\n");
}

function remove(map) {
    var arr = [];
    arr.push('');
    arr.push('    <!-- 按照ID删除 -->');
    arr.push('    <delete id="deleteById" parameterType="String">');
    arr.push('        DELETE FROM ' + map.tableName + ' WHERE id= #{id}');
    arr.push('    </delete>');
    arr.push('');
    return arr.join("\r\n");
}

function modify(map) {
    var arr = [];
    arr.push('');
    arr.push('    <!-- updateSampleById -->');
    arr.push('    <update id="updateSampleById" parameterType="' + map.attach.beanPackage + '.' + map.pascalName + '">');
    arr.push('        UPDATE ' + map.tableName);
    arr.push('        <trim prefix="SET" suffixOverrides=",">');

    var columns = map.columns;
    for (var i = 0; i < columns.length; i++) {
        var column = map.columns[i];
        arr.push('            <if test="' + column.camelName + ' != null">' + column.columnName + ' = #{' + column.camelName + '},</if>');
    }

    arr.push('        </trim>');
    arr.push('      WHERE id= #{id}');
    arr.push('    </update>');
    arr.push('');
    return arr.join("\r\n");
}

function modifyField(map) {
    var arr = [];
    arr.push('');
    arr.push('    <!-- updateFieldById -->');
    arr.push('    <update id="updateFieldById" parameterType="cn.seaboot.admin.mvc.argument.ModifyArgument">');
    arr.push('        UPDATE ' + map.tableName);

    var columns = map.columns;
    for (var i = 0; i < columns.length; i++) {
        var column = map.columns[i];
        arr.push('        <if test="field == \'' + column.camelName + '\'">SET ' + column.columnName + ' = #{value} </if>');
    }

    arr.push('        WHERE id= #{id}');
    arr.push('    </update>');
    arr.push('');
    return arr.join("\r\n");
}

function queryById(map) {
    var arr = [];
    arr.push('');
    arr.push('    <!-- 按照id查找 -->');
    arr.push('    <select id="selectById" parameterType="String" resultMap="' + map.camelName + '">');
    arr.push('        SELECT');
    arr.push('        <include refid="columns"/>');
    arr.push('        FROM ' + map.tableName + ' T1');
    arr.push('        WHERE T1.id= #{id}');
    arr.push('    </select>');
    arr.push('');
    return arr.join("\r\n");
}

function queryList(map) {
    var arr = [];
    arr.push('');
    arr.push('    <!-- 查询列表 -->');
    arr.push('    <select id="selectList" parameterType="map" resultMap="' + map.camelName + '">');
    arr.push('        SELECT');
    arr.push('        <include refid="columns"/>');
    arr.push('        FROM ' + map.tableName + ' T1');
    arr.push('        WHERE 1=1');
    arr.push('        <include refid="where_clause"/>');
    arr.push('    </select>');
    arr.push('');
    return arr.join("\r\n");
}

function footer(map) {
    return "</mapper>\r\n";
}

function main(map) {
    var arr = [];
    arr.push(header(map));
    arr.push(mapping(map));
    arr.push(columns(map));
    arr.push(where(map));
    arr.push(insert(map));
    arr.push(remove(map));
    arr.push(modify(map));
    arr.push(modifyField(map));
    arr.push(queryById(map));
    arr.push(queryList(map));
    arr.push(footer(map));
    return arr.join('');
}

export default main;
